package com.vegetable.modules.controller.finance;

import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.FinancialOther;
import com.vegetable.modules.entity.finance.po.FinancialDoneMainPO;
import com.vegetable.modules.service.finance.IFinancialOtherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 其他收款 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-15
 */
@Slf4j
@RestController
@RequestMapping("/financial/other")
@AllArgsConstructor
@Api(value = "其他收款", tags = "其他收款")
public class FinancialOtherController extends AbstractController {

    private IFinancialOtherService financialService;
    /**
     * 所有其他收款列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询其他收款列表", notes = "条件查询其他收款列表")
    public R list(@RequestParam Map<String, Object> params){
        // 状态0收款，1付款
        params.put("type", 0);
        log.info("条件查询其他收款列表{}", JSON.toJSONString(params));
        PageUtils page = financialService.queryPage(params);
        return R.data( page);
    }


    /**
     * 查询可增补实录数据列表
     */
    @GetMapping("/canAddedList")
    @ApiOperation(value = "查询可增补实录数据列表", notes = "查询可增补实录数据列表")
    public R canAddedList(){
        List<FinancialOther> list= financialService.canAddedList(0);
        return R.data(list);
    }

    /**
     * 新增其他收款
     */
    @SysLog(value = "新增其他收款")
    @PostMapping("/saveOther")
    @ApiOperation(value = "新增其他收款", notes = "新增其他收款")
    public R saveOther(@RequestBody FinancialOther financial){
        log.info("新增其他收款：{}", JSON.toJSONString(financial));
        financial.setType(0);
        financial.setCreateUserId(getUserId());
        try {
            return financialService.saveOther(financial);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 增补实录确定
     */
    @SysLog(value = "增补实录确定")
    @PostMapping("/done")
    @ApiOperation(value = "增补实录确定", notes = "增补实录确定")
    public R done(@RequestBody FinancialDoneMainPO po){
        log.info("新增其他收款：{}", JSON.toJSONString(po));
        if (po.getAccountId() == null){
            return R.fail("账户不能为空");
        }
        if (CollectionUtils.isEmpty(po.getItems())){
            return R.fail("增补实录数据不能为空");
        }
        try {
            return financialService.done(po);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除其他收款
     */
    @SysLog(value = "删除其他收款")
    @GetMapping("/deleteOther")
    @ApiOperation(value = "根据id删除其他收款", notes = "根据id删除其他收款")
    public R deleteOther(Long id){
        log.info("删除其他收款：{}", JSON.toJSONString(id));
        try {
            return financialService.deleteOther(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

}
